﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.Utility;

namespace EnvironmentManagerSystem
{
    public partial class BasicDataManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                ListBox1.SelectedIndexChanged += new EventHandler(ListBox1_SelectedIndexChanged);
                BindingBigKindList();
                if (!(Boolean)Session["Admin"])
                {
                    Button1.Visible = false;
                    Button2.Visible = false;
                    Button3.Visible = false;
                    DelSmallKind.Visible = false;
                    DelBigKind.Visible = false;
                    gdvUser.Columns[3].Visible = false;
                }
            }
        }

        #region 绑定大类数据
        /// <summary>
        /// 绑定大类数据
        /// </summary>
        private void BindingBigKindList()
        {
            ListBox1.Items.Clear();
            BasicDataMainDataCollection lst = BasicData.GetBigList();
            if (lst != null)
            {
                for (int i = 0; i < lst.Count; i++)
                {
                    ListBox1.Items.Add(lst[i].DataKindName);
                }
            }
            ListBox1.SelectedIndex = 0;
            string name = ListBox1.SelectedItem.Text.ToString();
            //int id = BasicData.GetMainIDbyName(name);
            gdvUser.DataSource = BasicData.GetSubKindByMainName(name);
            gdvUser.DataBind();
        }
        #endregion



        //分页事件
        protected void gdvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvUser.PageIndex = e.NewPageIndex;
        }

        //分页后事件
        protected void gdvUser_PageIndexChanged(object sender, EventArgs e)
        {
            BingSubKind();
        }

        //编辑事件
        protected void gdvUser_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdvUser.EditIndex = e.NewEditIndex;
            BingSubKind();
        }

        //取消事件
        protected void gdvUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdvUser.EditIndex = -1;
            BingSubKind();
        }

        //更新事件
        protected void gdvUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            BasicDataSubData model = new BasicDataSubData();
            model.DataValue = ((TextBox)gdvUser.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
            model.DataValueMemo = ((TextBox)gdvUser.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
            model.ID = Convert.ToInt32((gdvUser.Rows[e.RowIndex].Cells[0]).Text.Trim());
            if (BasicData.UpDataSubKindByID(model) > 0)
            {
                Jscript.Alert("修改成功", this);
                gdvUser.EditIndex = -1;
                BingSubKind();
            }
            else
            {
                Jscript.Alert("修改失败", this);
            }
        }

        //删除事件
        protected void gdvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (BasicData.DeleteSubKind(gdvUser.DataKeys[e.RowIndex].Value.ToString()) > 0)
            {
                Jscript.Alert("删除成功", this);
                BingSubKind();
            }
            else
            {
                Jscript.Alert("删除失败", this);
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BingSubKind();
        }

        private void BingSubKind()
        {
            if (ListBox1.SelectedItem != null)
            {
                string name = ListBox1.SelectedItem.Text.ToString();
                //int id = BasicData.GetMainIDbyName(name);
                gdvUser.DataSource = BasicData.GetSubKindByMainName(name);
                gdvUser.DataBind();
            }
        }

        protected void DelBigKind_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedItem == null)
            {
                Jscript.Alert("请选择要删除的大类", this);
                return;
            }
            if (BasicData.DelMainKindByName(ListBox1.SelectedItem.Text.Trim()) > 0)
            {
                Jscript.Alert("删除成功", this);
                BindingBigKindList();
            }
            else
            {
                Jscript.Alert("删除失败", this);

            }

        }

        //刷新数据
        protected void DelSmallKind_Click(object sender, EventArgs e)
        {
            BindingBigKindList();
        }

        protected void gdvUser_DataBound(object sender, EventArgs e)
        {
            OfficeHelper.HelpDv(gdvUser);
        }
    }
}
